
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class CancionCAD : BasicCAD, ICancionCAD
        {
        public CancionCAD() : base ()
        {
        }

        public CancionCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public CancionEN ReadOIDDefault (int id)
        {
                CancionEN cancionEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        cancionEN = (CancionEN)session.Load (typeof(CancionEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return cancionEN;
        }


        public int New_ (CancionEN cancion)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (cancion.Artista != null) {
                                for (int i = 0; i < cancion.Artista.Count; i++)
                                        cancion.Artista [i] = (SpotifyGenNHibernate.EN.Spotify.ArtistaEN)session.Load (typeof(SpotifyGenNHibernate.EN.Spotify.ArtistaEN), cancion.Artista [i].Id);
                        }
                        if (cancion.Albumes != null) {
                                for (int i = 0; i < cancion.Albumes.Count; i++)
                                        cancion.Albumes [i] = (SpotifyGenNHibernate.EN.Spotify.AlbumEN)session.Load (typeof(SpotifyGenNHibernate.EN.Spotify.AlbumEN), cancion.Albumes [i].Id);
                        }

                        session.Save (cancion);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return cancion.Id;
        }

        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        CancionEN cancionEN = (CancionEN)session.Load (typeof(CancionEN),
                                id

                                );
                        session.Delete (cancionEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public void Editar (CancionEN cancion)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        CancionEN cancionEN = (CancionEN)session.Load (typeof(CancionEN), cancion.Id);

                        cancionEN.Nombre = cancion.Nombre;


                        cancionEN.Duracion = cancion.Duracion;


                        cancionEN.Genero = cancion.Genero;


                        cancionEN.Comprable = cancion.Comprable;


                        cancionEN.Precio = cancion.Precio;


                        cancionEN.FechaLanzamiento = cancion.FechaLanzamiento;


                        cancionEN.Year = cancion.Year;


                        cancionEN.Disponible = cancion.Disponible;

                        session.Update (cancionEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        public System.Collections.Generic.IList<CancionEN> Buscar (int first, int size)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(CancionEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<CancionEN>();
                        else
                                result = session.CreateCriteria (typeof(CancionEN)).List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }

        public System.Collections.Generic.IList<CancionEN> ConsultarParaRadio (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> FiltrarPorAlbum (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorCancionesParaMi (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorCancionesEnMiPais (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorCancionesEnMiContinente (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorArtistasParaMi (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorArtistasEnMiPais (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorArtistasEnMiContinente (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorAlbumesParaMi (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorAlbumesEnMiPais (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        public System.Collections.Generic.IList<CancionEN> ConsultarListasDeExitosPorAlbumesEnMiContinente (int p_oid)
        {
                System.Collections.Generic.IList<CancionEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        String sql = @"FROM CancionEN self ";
                        IQuery query = session.CreateQuery (sql);
                        result = query.List<CancionEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in CancionCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        }
}
